add leading zeros in php

33

php add 0 before number -

$number = 4;
str_pad($number, 2, '0', STR_PAD_LEFT); // returns 04

$number = 14;
str_pad($number, 2, '0', STR_PAD_LEFT); // returns 14

prepend 0 to number php -

str_pad($month, 2, '0', STR_PAD_LEFT); 

add zeros in front of number php -

<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>

format a number with leading zeros in php -

sprintf('%06d', '12')

Comments

Submit
0 Comments